import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        Code: null,
        title: '发送验证码',
        titleConst: "发送验证码", // 重置后的按钮中显示的字符
        count: 60,
        countConst: 60, // 重置后的倒计时的秒数
        isDisabled: false,
        files: [],
        faceFeature: null,
        faceBase64String: null
    },
    onLoad() {
    },
    async selectFile(e) {
        var file = e.detail
        try {
            let imgBase64 = wx.getFileSystemManager().readFileSync(file.tempFilePaths[0], "base64")
            const res = await wx.$Api.face.Check({
                data: {
                    ExcludeId: '',
                    FaceBase64String: imgBase64
                },
                hasLoading: true
            })
            this.data.faceFeature = res.Data.FaceFeature
            this.data.faceBase64String ='data:image/jpg;base64,' +  res.Data.FaceBase64String
            this.setData({
                files: [{ url: file.tempFilePaths[0] }]
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
    uploadError(e) {
    },
    deleteFile(e) {
        var index = e.detail.index
        const files = this.data.files
        files.splice(index, 1)
        this.setData({
            files
        })
        this.data.faceFeature = null
        this.data.faceBase64String = null
    },
    handlePhoneInput(e) {
        this.setData({
            Phone: e.detail.value
        })
    },
    async handleSendCode(e) {
        if (this.data.isDisabled) {
            return
        }
        if (!this.data.Phone || this.data.Phone.length != 11) {
            return
        }

        try {
            await wx.$Api.member.SendCode({
                data: { Phone: this.data.Phone }
            })

            this.countdown()
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }

    },
    countdown() {
        let count = this.data.count;
        // 当count不为0开始倒计时，当count为0就关闭倒计时
        // 设置定时器
        var countdown = setInterval(() => {
            if (count <= 0) {
                this.setData({
                    title: this.data.titleConst,
                    count: this.data.countConst,
                    isDisabled: false
                });
                // 取消定时器
                clearInterval(countdown);
            } else {
                this.setData({
                    title: count-- + "s后重试",
                    isDisabled: true
                });
            }
        }, 1000);
    },
    async submitForm(e) {
        const params = e.detail.value
        if (!params.Phone) {
            wx.$Fun.showToast('手机号不能为空')
            return
        }
        if (!params.Code) {
            wx.$Fun.showToast('验证码不能为空')
            return
        }
        if (!params.Name) {
            wx.$Fun.showToast('姓名不能为空')
            return
        }
        if (!this.data.faceBase64String) {
            wx.$Fun.showToast('头像不能为空')
            return
        }
        try {
            const res = await wx.$Api.member.HelpRegister({
                data: {
                    Phone: params.Phone,
                    Code: params.Code,
                    Name: params.Name,
                    FaceBase64String: this.data.faceBase64String,
                    FaceFeature: this.data.faceFeature
                }
            })

            //弹框提示是否继续支付
            wx.showModal({
                content: '继续办理包月套餐吗？',
                success: function (e) {
                    if (e.confirm) {
                        //跳转到办理页面
                        wx.$Fun.toPage({
                            page: 'vehicleAddSearch',
                            params: {
                                "0": res.Data,
                                pageType: 'redirectTo'
                            }
                        })
                    }
                    else if (e.cancel) {
                        //跳转到首页
                        wx.$Fun.toPage({
                            page: 'mine',
                            params: {
                                pageType: 'redirectTo'
                            }
                        })
                    }
                },
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
}))